<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input
              v-model="deptName"
              placeholder="请输入部门名称"
              clearable
              size="small"
              prefix-icon="el-icon-search"
              style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container">
          <el-tree
              :data="deptOptions"
              :props="defaultProps"
              :expand-on-click-node="false"
              :filter-node-method="filterNode"
              ref="tree"
              node-key="id"
              default-expand-all
              highlight-current
              @node-click="handleNodeClick"
          />
        </div>
      </el-col>
      <!--用户数据-->
      <el-col :span="20" :xs="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
                 label-width="68px">
            <el-form-item label="用户昵称" prop="nickName">
                <el-input
                        v-model="queryParams.nickName"
                        placeholder="请输入用户昵称"
                        clearable
                        @keyup.enter.native="handleQuery"
                />
            </el-form-item>
          <el-form-item label="用户账号" prop="username">
            <el-input
                v-model="queryParams.username"
                placeholder="请输入用户账号"
                clearable
                @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="手机号" prop="phone">
            <el-input
                v-model="queryParams.phone"
                placeholder="请输入手机号"
                clearable
                @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
        <el-table v-loading="loading" :data="userList">
          <el-table-column label="用户昵称" align="center" prop="nickName"/>
          <el-table-column label="用户账号" align="center" prop="username"/>
          <el-table-column label="手机号" align="center" prop="phone"/>
          <el-table-column label="所属部门" align="center" prop="deptName"/>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="resetPassword(scope.row)"
                  v-hasPermi="['common:user:reset']"
              >重置密码
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>
    <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageIndex"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
    />

  </div>
</template>

<script>

import {pageUser, resetPassword} from "@/api/business/mpuser/user";
import {deptTreeSelect} from "@/api/system/user";

export default {
  name: "MpUser",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 部门树选项
      deptOptions: undefined,
      // 部门名称
      deptName: undefined,
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 总条数
      total: 0,
      // 小程序用户表格数据
      userList: [],
      // 查询参数
      queryParams: {
        pageIndex: 1,
        pageSize: 10,
        nickName: null,
        username: null,
        phone: null,
        deptId: null
      },
    };
  },
  created() {
    this.getList();
    this.getDeptTree();
  },
  methods: {
    /** 查询通知列表 */
    getList() {
      this.loading = true;
      pageUser(this.queryParams).then(response => {
        this.userList = response.data.records;
        this.total = response.data.records.total;
        this.loading = false;
      });
    },
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then(response => {
        this.deptOptions = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageIndex = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    resetPassword(row) {
      const id = row.id
      resetPassword({id: id}).then(response => {
        this.$modal.msgSuccess("重置密码成功");
        this.open = false;
        this.getList();
      })
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.deptId = data.id;
      this.handleQuery();
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('common/notice/export', {
        ...this.queryParams
      }, `小程序用户.xlsx`)
    },
  }
};
</script>

